class Solution:
    def containsNearbyAlmostDuplicate(self, nums, k: int, t: int) -> bool:
        arr = []
        if len(nums) <= 1:
            return False
        arr.append(nums[0])
        start = 0
        for end in range(1, len(nums)):
            if end - start > k:
                arr.remove(nums[start])
                start = start + 1
            tmp = len(arr)
            for i in range(len(arr)):
                if arr[i] > nums[end]:
                    tmp = i
                    break
            if tmp - 1 >= 0 and nums[end] - arr[tmp - 1] <= t:
                return True
            if tmp < len(arr) and arr[tmp] - nums[end] <= t:
                return True
            arr.insert(tmp, nums[end])
        return False


if __name__ == '__main__':
    so = Solution()
    nums = [1,5,9,1,5,9]
    k = 2
    t = 3
    r = so.containsNearbyAlmostDuplicate(nums, k, t)
    print(r)
